package com.wolfpire.workflow.platform;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.system.common.Constants;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.Role;
import com.wolfpire.system.model.User;
import com.wolfpire.system.service.OrgService;
import com.wolfpire.system.service.RoleService;
import com.wolfpire.system.service.UserService;


@Service("platformService")
public class PlatformService {
	
	@Resource private OrgService orgService;
	
	@Resource private RoleService roleService;	
	
	@Resource private UserService userService;

	//----------------
	public User getSessionUser(HttpServletRequest request) throws Exception {
		User user = (User) request.getSession().getAttribute(Constants.SESSION_USER);

		return user;
		
	}
	
	public List<Authority> getSessionUserAuthorities(HttpServletRequest request) throws Exception{
		
		return getSessionUser(request).getUserAuthorities();
	}
	

	public String getOrgTree() {
		List<Org> orgList = orgService.list(null);
		for(Org org : orgList){
			if(org.getParentId() == null){
				//TODO:
//				return JsonHelper.getJsonString(buildTree(org.getId(), orgList));
				return null;
			}
		}
		return null;
	}

	
	@SuppressWarnings("unused")
	private List<Map<String, Object>> buildTree(Object parentId, List<Org> orgList){
		
		List<Map<String, Object>> orgs = new ArrayList<Map<String, Object>>();
		for(Org org : orgList){
			if(parentId.equals(org.getParentId())){
				Map<String, Object> node = new HashMap<String, Object>();
				node.put("id", org.getId());
				node.put("parentId", org.getParentId());
				node.put("text", org.getName());
				node.put("code", org.getCode());
				List<Map<String, Object>> children = buildTree(org.getId(), orgList);
				node.put("children", children);
				orgs.add(node);
			}
		}
		
		return orgs;
	}
	
	
	public List<Role> getRoleTree() {
		return this.roleService.list(null);
	}
	
	@SuppressWarnings("unused")
	private List<Map<String, Object>> buildRoleTree(List<Role> roleList){
		
		List<Map<String, Object>> orgs = new ArrayList<Map<String, Object>>();
		for(Role role : roleList){
			Map<String, Object> node = new HashMap<String, Object>();
			node.put("id", role.getId());
			//node.put("parentId", org.getParentId());
			node.put("text", role.getName());
			//node.put("code", role.get);
			orgs.add(node);
		}
		
		return orgs;
	}
	
	public String getUserTree(Long orgId) {
		
		/*List<User> userList = userService.queryUsers(orgId);
		List<Map<String, Object>> users = new ArrayList<Map<String, Object>>();
		for(User user : userList){
			Map<String, Object> node = new HashMap<String, Object>();
			node.put("id", user.getId());
			//node.put("parentId", org.getParentId());
			node.put("text", user.getNickname());
			//node.put("code", role.get);
			users.add(node);
		}
		return JsonHelper.getJsonString(users);*/
		//TODO:
		return null;
	}

	public List<Org> getCurOrgTree(Long orgId) {
		Org org = new Org();
		org.setParentId(orgId);
		return orgService.list(org);
	}
	
	public List<Map<String, Object>> getUserListByOrgId(Long orgId) {
		//TODO:
//		return userService.queryUserList(orgId);
		return null;
	}

	
	public List<Map<String, Object>> getUserListByRoleId(Long roleId) {
		//TODO:
//		return userService.queryUserListByRoleId(roleId);
		return null;
	}

	public List<User> getUsersByRoleId(Long roleId) {
		return this.userService.getUsersByRoleId(roleId);
	}

	public List<User> getUsersByOrgId(Long orgId) {
		return this.userService.getUsersByOrgId(orgId);
	}

	public List<User> getUsersByNickNameAndRole(String nickName,
			List<Long> roleIds) {
		User user = new User();
		user.setNickName(StringUtils.stripToEmpty(nickName));
		return this.userService.getUsersByParam(user , roleIds, null);
	}
	
}
